package com.fenghuajueli.module_user.mine

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.fenghuajueli.libbasecoreui.baseswitch.PublicFunction
import com.fenghuajueli.libbasecoreui.constants.EventBusConstants
import com.fenghuajueli.libbasecoreui.data.entity.eventbus.EventEntity
import com.fenghuajueli.libbasecoreui.dialog.CommonTipsDialog
import com.fenghuajueli.libbasecoreui.feedback.FeedbackPrevActivity
import com.fenghuajueli.libbasecoreui.feedback.ReportFeedbackActivity
import com.fenghuajueli.libbasecoreui.mvp.BaseFragment
import com.fenghuajueli.libbasecoreui.ui.CustomerServiceActivity
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils
import com.fenghuajueli.libbasecoreui.utils.JumpActivityUtils
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils
import com.fenghuajueli.module_route.PermissionCenterModuleRoute
import com.fenghuajueli.module_route.UserModuleRoute
import com.fenghuajueli.module_user.ui.helpcenter.HelpCenterActivity
import com.fenghuajueli.module_user.model.RequestInfoModel
import com.sy.module_permission_center_hmy.ThirdManifestActivity
import com.sy.module_permission_center_hmy.collectlist.PersonInfoCollectionListActivity
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @Author: sz_ijj
 * @Date: 2024-01-06 14:56
 **/
open class BaseMineFragment : BaseFragment() {
    val viewModel: UserMineViewModel by viewModels()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        EventBus.getDefault().register(this)
        lifecycleScope.launch {
            viewModel.isLoading
                .flowWithLifecycle(lifecycle, Lifecycle.State.CREATED)
                .collectLatest {
                    if (it) {
                        hideLoadingDialog()
                        ToastUtils.showShort("清理成功")
                    }
                }
        }
    }

    fun toUserInfoContainer() {
        if (UserInfoUtils.getInstance().isLogin || !MmkvUtils.get(
                RequestInfoModel.LOGIN_OUT,
                false
            )
        ) {
            ARouter.getInstance().build(UserModuleRoute.USER_INFO_ACTIVITY).navigation()
        } else {
            ARouter.getInstance().build(UserModuleRoute.USER_LOGIN).navigation()
        }
    }

    fun toVipContainer() {
        ARouter.getInstance().build(UserModuleRoute.USER_OPEN_VIP_ACTIVITY).navigation()
    }

    fun toComment() {
        PublicFunction.goComment(context)
    }

    fun toUserFeedBack() {
        viewModel.saveDot()
//        JumpActivityUtils.goNormalActivity(context, FeedbackPrevActivity::class.java)
        HelpCenterActivity.start(requireContext())
    }

    fun toClearCache() {
        showCommTipDialog()
    }

    fun toAboutUs() {
        JumpActivityUtils.goAboutUsActivity(requireContext())
    }

    fun toContactServer() {
        CustomerServiceActivity.start(requireContext())
    }

    fun toAllAgreement() {
        ARouter.getInstance().build(PermissionCenterModuleRoute.PRIVACY_SETTING_ACTIVITY)
            .navigation()
    }

    fun toReportFeedBack() {
        JumpActivityUtils.goNormalActivity(requireContext(), ReportFeedbackActivity::class.java)
    }

    fun toInfoCollectList() {
        PersonInfoCollectionListActivity.start(requireContext())
    }

    fun toThirdSdkList() {
        ThirdManifestActivity.start(requireContext())
    }


    override fun onResume() {
        super.onResume()
        viewModel.refreshInfo()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun event(eventBusEntity: EventEntity) {
        if (eventBusEntity.code == EventBusConstants.REGISTER_SUCCESS) {
            viewModel.refreshInfo()
        }
        if (eventBusEntity.code == EventBusConstants.LOGIN_SUCCESS) {
            viewModel.refreshInfo()
        }
        if (eventBusEntity.code == EventBusConstants.LOGIN_OUT) {
            viewModel.refreshInfo()
        }
        if (eventBusEntity.code == EventBusConstants.DELETE_USER) {
            viewModel.refreshInfo()
        }
        if (eventBusEntity.code == EventBusConstants.UPDATE_USER_INFO) {
            viewModel.refreshInfo()
        }
    }

    private var commonTipsDialog: CommonTipsDialog? = null

    private fun showCommTipDialog() {
        hideCommTipDialog()
        commonTipsDialog = CommonTipsDialog(requireContext(), "你确定要清除缓存吗？")
        commonTipsDialog?.apply {
            setCancelListener {
                hideCommTipDialog()
            }
            setConfirmListener {
                hideCommTipDialog()
                showLoadingDialog()
                viewModel.fakeLoading()
            }
            show()
        }
    }

    private fun hideCommTipDialog() {
        commonTipsDialog?.dismiss()
    }
}